﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Data;

namespace Gpsys.Common.Helper
{
    public static class XmlHelper
    {
        public static string SerializeToXml(object AnObject)
        {
            return SerializeToXml(AnObject, "", "");
        }

        public static string SerializeToXml(object AnObject, string rootName, string rootNamespace)
        {
            XmlWriterSettings writerSettings = new XmlWriterSettings();
            writerSettings.OmitXmlDeclaration = true;
            writerSettings.Indent = true;

            XmlSerializer Xml_Serializer = null;

            if (!string.IsNullOrEmpty(rootName) && !string.IsNullOrEmpty(rootNamespace))
            {
                Xml_Serializer = new XmlSerializer(AnObject.GetType(),
                    new XmlRootAttribute()
                        {
                            ElementName = rootName,
                            Namespace = rootNamespace
                        }
                    );
            }
            else
            {
                Xml_Serializer = new XmlSerializer(AnObject.GetType());
            }

            StringWriter stringWriter = new StringWriter();

            using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, writerSettings))
            {
                Xml_Serializer.Serialize(xmlWriter, AnObject);
            }
            return stringWriter.ToString();
        }

        public static object DeserializeFromXml(string xml, Type type)
        {
            return DeserializeFromXml(xml, "", "", type);
        }

        public static object DeserializeFromXml(string xml, string rootName, string rootNamespace, Type type)
        {
            object result;
            XmlSerializer ser;
            if (!string.IsNullOrEmpty(rootName) && !string.IsNullOrEmpty(rootNamespace))
            {
                ser = new XmlSerializer(type,
                     new XmlRootAttribute()
                     {
                         ElementName = rootName,
                         Namespace = rootNamespace
                     });

            }
            else
            {
                ser = new XmlSerializer(type);
            }

            using (TextReader tr = new StringReader(xml))
            {
                result = ser.Deserialize(tr);
            }
            return result;
        }


        public static string SerializeToXml(DataTable dt, string rootName, string recordName, string rootNamespace)
        {
            dt.TableName = recordName;
            dt.Namespace = rootNamespace;

            var ds = new DataSet();
            ds.DataSetName = rootName;
            ds.Namespace = rootNamespace;

            ds.Tables.Add(dt);

            MemoryStream ms = new MemoryStream();
            ds.WriteXml(ms);

            ms.Position = 0;
            StreamReader reader = new StreamReader(ms);
            return reader.ReadToEnd();
        }

    }
}
